home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / printPageSetup.js < prev    next >
Encoding:
JavaScript  |  2007-03-10  |  15.8 KB  |  478 lines

  1. //@line 40 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/components/printing/content/printPageSetup.js"
  2.  
  3. var gDialog;
  4. var paramBlock;
  5. var gPrefs         = null;
  6. var gPrintService  = null;
  7. var gPrintSettings = null;
  8. var gStringBundle  = null;
  9. var gDoingMetric   = false;
  10.  
  11. var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings;
  12. var gDoDebug = false;
  13.  
  14. //---------------------------------------------------
  15. function initDialog()
  16. {
  17.   gDialog = new Object;
  18.  
  19.   gDialog.orientation     = document.getElementById("orientation");
  20.   gDialog.portrait        = document.getElementById("portrait");
  21.   gDialog.landscape       = document.getElementById("landscape");
  22.  
  23.   gDialog.printBG         = document.getElementById("printBG");
  24.  
  25.   gDialog.shrinkToFit     = document.getElementById("shrinkToFit");
  26.  
  27.   gDialog.marginGroup     = document.getElementById("marginGroup");
  28.  
  29.   gDialog.marginPage      = document.getElementById("marginPage");
  30.   gDialog.marginTop       = document.getElementById("marginTop");
  31.   gDialog.marginBottom    = document.getElementById("marginBottom");
  32.   gDialog.marginLeft      = document.getElementById("marginLeft");
  33.   gDialog.marginRight     = document.getElementById("marginRight");
  34.  
  35.   gDialog.topInput        = document.getElementById("topInput");
  36.   gDialog.bottomInput     = document.getElementById("bottomInput");
  37.   gDialog.leftInput       = document.getElementById("leftInput");
  38.   gDialog.rightInput      = document.getElementById("rightInput");
  39.  
  40.   gDialog.hLeftOption     = document.getElementById("hLeftOption");
  41.   gDialog.hCenterOption   = document.getElementById("hCenterOption");
  42.   gDialog.hRightOption    = document.getElementById("hRightOption");
  43.  
  44.   gDialog.fLeftOption     = document.getElementById("fLeftOption");
  45.   gDialog.fCenterOption   = document.getElementById("fCenterOption");
  46.   gDialog.fRightOption    = document.getElementById("fRightOption");
  47.  
  48.   gDialog.scalingLabel    = document.getElementById("scalingInput");
  49.   gDialog.scalingInput    = document.getElementById("scalingInput");
  50.  
  51.   gDialog.enabled         = false;
  52.  
  53.   gDialog.strings                          = new Array;
  54.   gDialog.strings[ "marginUnits.inches" ]  = document.getElementById("marginUnits.inches").childNodes[0].nodeValue;
  55.   gDialog.strings[ "marginUnits.metric" ]  = document.getElementById("marginUnits.metric").childNodes[0].nodeValue;
  56.   gDialog.strings[ "customPrompt.title" ]  = document.getElementById("customPrompt.title").childNodes[0].nodeValue;
  57.   gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue;
  58.  
  59. }
  60.  
  61. //---------------------------------------------------
  62. function isListOfPrinterFeaturesAvailable()
  63. {
  64.   var has_printerfeatures = false;
  65.   
  66.   try {
  67.     has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures");
  68.   } catch(ex) {
  69.   }
  70.   
  71.   return has_printerfeatures;
  72. }
  73.  
  74. //---------------------------------------------------
  75. function checkDouble(element)
  76. {
  77.   element.value = element.value.replace(/[^.0-9]/g, "");
  78. }
  79.  
  80. // Theoretical paper width/height.
  81. var gPageWidth  = 8.5;
  82. var gPageHeight = 11.0;
  83.  
  84. //---------------------------------------------------
  85. function setOrientation()
  86. {
  87.   var selection = gDialog.orientation.selectedItem;
  88.  
  89.   var style = "background-color:white;";
  90.   if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || 
  91.       (selection == gDialog.landscape && gPageWidth < gPageHeight)) {
  92.     // Swap width/height.
  93.     var temp = gPageHeight;
  94.     gPageHeight = gPageWidth;
  95.     gPageWidth = temp;
  96.   }
  97.   var div = gDoingMetric ? 100 : 10;
  98.   style += "width:" + gPageWidth/div + unitString() + ";height:" + gPageHeight/div + unitString() + ";";
  99.   gDialog.marginPage.setAttribute( "style", style );
  100. }
  101.  
  102. //---------------------------------------------------
  103. function unitString()
  104. {
  105.   return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm";
  106. }
  107.  
  108. //---------------------------------------------------
  109. function checkMargin( value, max, other )
  110. {
  111.   // Don't draw this margin bigger than permitted.
  112.   return Math.min(value, max - other.value);
  113. }
  114.  
  115. //---------------------------------------------------
  116. function changeMargin( node )
  117. {
  118.   // Correct invalid input.
  119.   checkDouble(node);
  120.  
  121.   // Reset the margin height/width for this node.
  122.   var val = node.value;
  123.   var nodeToStyle;
  124.   var attr="width";
  125.   if ( node == gDialog.topInput ) {
  126.     nodeToStyle = gDialog.marginTop;
  127.     val = checkMargin( val, gPageHeight, gDialog.bottomInput );
  128.     attr = "height";
  129.   } else if ( node == gDialog.bottomInput ) {
  130.     nodeToStyle = gDialog.marginBottom;
  131.     val = checkMargin( val, gPageHeight, gDialog.topInput );
  132.     attr = "height";
  133.   } else if ( node == gDialog.leftInput ) {
  134.     nodeToStyle = gDialog.marginLeft;
  135.     val = checkMargin( val, gPageWidth, gDialog.rightInput );
  136.   } else {
  137.     nodeToStyle = gDialog.marginRight;
  138.     val = checkMargin( val, gPageWidth, gDialog.leftInput );
  139.   }
  140.   var style = attr + ":" + (val/10) + unitString() + ";";
  141.   nodeToStyle.setAttribute( "style", style );
  142. }
  143.  
  144. //---------------------------------------------------
  145. function changeMargins()
  146. {
  147.   changeMargin( gDialog.topInput );
  148.   changeMargin( gDialog.bottomInput );
  149.   changeMargin( gDialog.leftInput );
  150.   changeMargin( gDialog.rightInput );
  151. }
  152.  
  153. //---------------------------------------------------
  154. function customize( node )
  155. {
  156.   // If selection is now "Custom..." then prompt user for custom setting.
  157.   if ( node.value == 6 ) {
  158.     var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  159.                      .getService( Components.interfaces.nsIPromptService );
  160.     var title      = gDialog.strings[ "customPrompt.title" ];
  161.     var promptText = gDialog.strings[ "customPrompt.prompt" ];
  162.     var result = { value: node.custom };
  163.     var ok = prompter.prompt(window, title, promptText, result, null, { value: false } );
  164.     if ( ok ) {
  165.         node.custom = result.value;
  166.     }
  167.   }
  168. }
  169.  
  170. //---------------------------------------------------
  171. function setHeaderFooter( node, value )
  172. {
  173.   node.value= hfValueToId(value);
  174.   if (node.value == 6) {
  175.     // Remember current Custom... value.
  176.     node.custom = value;
  177.   } else {
  178.     // Start with empty Custom... value.
  179.     node.custom = "";
  180.   }
  181. }
  182.  
  183. var gHFValues = new Array;
  184. gHFValues[ "&T" ] = 1;
  185. gHFValues[ "&U" ] = 2;
  186. gHFValues[ "&D" ] = 3;
  187. gHFValues[ "&P" ] = 4;
  188. gHFValues[ "&PT" ] = 5;
  189.  
  190. function hfValueToId(val)
  191. {
  192.   if ( val in gHFValues ) {
  193.       return gHFValues[val];
  194.   }
  195.   if ( val.length ) {
  196.       return 6; // Custom...
  197.   } else {
  198.       return 0; // --blank--
  199.   }
  200. }
  201.  
  202. function hfIdToValue(node)
  203. {
  204.   var result = "";
  205.   switch ( parseInt( node.value ) ) {
  206.   case 0:
  207.     break;
  208.   case 1:
  209.     result = "&T";
  210.     break;
  211.   case 2:
  212.     result = "&U";
  213.     break;
  214.   case 3:
  215.     result = "&D";
  216.     break;
  217.   case 4:
  218.     result = "&P";
  219.     break;
  220.   case 5:
  221.     result = "&PT";
  222.     break;
  223.   case 6:
  224.     result = node.custom;
  225.     break;
  226.   }
  227.   return result;
  228. }
  229.  
  230. function setPrinterDefaultsForSelectedPrinter()
  231. {
  232.   if (gPrintSettings.printerName == "") {
  233.     gPrintSettings.printerName = gPrintService.defaultPrinterName;
  234.   }
  235.   
  236.   // First get any defaults from the printer 
  237.   gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
  238.  
  239.   // now augment them with any values from last time
  240.   gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll);
  241.   
  242.   if (gDoDebug) {
  243.     dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n");
  244.   }
  245. }
  246.  
  247. //---------------------------------------------------
  248. function loadDialog()
  249. {
  250.   var print_orientation   = 0;
  251.   var print_margin_top    = 0.5;
  252.   var print_margin_left   = 0.5;
  253.   var print_margin_bottom = 0.5;
  254.   var print_margin_right  = 0.5;
  255.  
  256.   try {
  257.     gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  258.   
  259.     gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
  260.     if (gPrintService) {
  261.       gPrintService = gPrintService.getService();
  262.       if (gPrintService) {
  263.         gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
  264.       }
  265.     }
  266.   } catch(ex) {
  267.     dump("loadDialog: ex="+ex+"\n");
  268.   }
  269.  
  270.   setPrinterDefaultsForSelectedPrinter();
  271.  
  272.   gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages;
  273.  
  274.   gDialog.shrinkToFit.checked   = gPrintSettings.shrinkToFit;
  275.  
  276.   gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked;
  277.  
  278.   var marginGroupLabel = gDialog.marginGroup.label;
  279.   if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) {
  280.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]);
  281.     gDoingMetric = false;
  282.   } else {
  283.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]);
  284.     // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point).
  285.     gPageWidth = 2100;
  286.     gPageHeight = 2970;
  287.     gDoingMetric = true;
  288.   }
  289.   gDialog.marginGroup.label = marginGroupLabel;
  290.  
  291.   print_orientation   = gPrintSettings.orientation;
  292.   print_margin_top    = convertMarginInchesToUnits(gPrintSettings.marginTop, gDoingMetric);
  293.   print_margin_left   = convertMarginInchesToUnits(gPrintSettings.marginLeft, gDoingMetric);
  294.   print_margin_right  = convertMarginInchesToUnits(gPrintSettings.marginRight, gDoingMetric);
  295.   print_margin_bottom = convertMarginInchesToUnits(gPrintSettings.marginBottom, gDoingMetric);
  296.  
  297.   if (gDoDebug) {
  298.     dump("print_orientation   "+print_orientation+"\n");
  299.  
  300.     dump("print_margin_top    "+print_margin_top+"\n");
  301.     dump("print_margin_left   "+print_margin_left+"\n");
  302.     dump("print_margin_right  "+print_margin_right+"\n");
  303.     dump("print_margin_bottom "+print_margin_bottom+"\n");
  304.   }
  305.  
  306.   if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) {
  307.     gDialog.orientation.selectedItem = gDialog.portrait;
  308.   } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) {
  309.     gDialog.orientation.selectedItem = gDialog.landscape;
  310.   }
  311.  
  312.   // Set orientation the first time on a timeout so the dialog sizes to the
  313.   // maximum height specified in the .xul file.  Otherwise, if the user switches
  314.   // from landscape to portrait, the content grows and the buttons are clipped.
  315.   setTimeout( setOrientation, 0 );
  316.  
  317.   gDialog.topInput.value    = print_margin_top.toFixed(1);
  318.   gDialog.bottomInput.value = print_margin_bottom.toFixed(1);
  319.   gDialog.leftInput.value   = print_margin_left.toFixed(1);
  320.   gDialog.rightInput.value  = print_margin_right.toFixed(1);
  321.   changeMargins();
  322.  
  323.   setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft );
  324.   setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter );
  325.   setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight );
  326.  
  327.   setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft );
  328.   setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter );
  329.   setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight );
  330.  
  331.   gDialog.scalingInput.value = (gPrintSettings.scaling * 100).toFixed(0);
  332.  
  333.   // Enable/disable widgets based in the information whether the selected
  334.   // printer supports the matching feature or not
  335.   if (isListOfPrinterFeaturesAvailable()) {
  336.     if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation"))
  337.       gDialog.orientation.removeAttribute("disabled");
  338.     else
  339.       gDialog.orientation.setAttribute("disabled","true");
  340.   }
  341.  
  342.   // Give initial focus to the orientation radio group.
  343.   // Done on a timeout due to to bug 103197.
  344.   setTimeout( function() { gDialog.orientation.focus(); }, 0 );
  345. }
  346.  
  347. //---------------------------------------------------
  348. function onLoad()
  349. {
  350.   // Init gDialog.
  351.   initDialog();
  352.  
  353.   if (window.arguments[0] != null) {
  354.     gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings);
  355.     paramBlock     = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  356.   } else if (gDoDebug) {
  357.     alert("window.arguments[0] == null!");
  358.   }
  359.  
  360.   // default return value is "cancel"
  361.   paramBlock.SetInt(0, 0);
  362.  
  363.   if (gPrintSettings) {
  364.     loadDialog();
  365.   } else if (gDoDebug) {
  366.     alert("Could initialize gDialog, PrintSettings is null!");
  367.   }
  368. }
  369.  
  370. function convertUnitsMarginToInches(aVal, aIsMetric)
  371. {
  372.   if (aIsMetric) {
  373.     return aVal / 25.4;
  374.   } else {
  375.     return aVal;
  376.   }
  377. }
  378.  
  379. function convertMarginInchesToUnits(aVal, aIsMetric)
  380. {
  381.   if (aIsMetric) {
  382.     return aVal * 25.4;
  383.   } else {
  384.     return aVal;
  385.   }
  386. }
  387.  
  388. //---------------------------------------------------
  389. function onAccept()
  390. {
  391.  
  392.   if (gPrintSettings) {
  393.     if ( gDialog.orientation.selectedItem == gDialog.portrait ) {
  394.       gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation;
  395.     } else {
  396.       gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation;
  397.     }
  398.  
  399.     // save these out so they can be picked up by the device spec
  400.     gPrintSettings.marginTop    = convertUnitsMarginToInches(gDialog.topInput.value, gDoingMetric);
  401.     gPrintSettings.marginLeft   = convertUnitsMarginToInches(gDialog.leftInput.value, gDoingMetric);
  402.     gPrintSettings.marginBottom = convertUnitsMarginToInches(gDialog.bottomInput.value, gDoingMetric);
  403.     gPrintSettings.marginRight  = convertUnitsMarginToInches(gDialog.rightInput.value, gDoingMetric);
  404.  
  405.     gPrintSettings.headerStrLeft   = hfIdToValue(gDialog.hLeftOption);
  406.     gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption);
  407.     gPrintSettings.headerStrRight  = hfIdToValue(gDialog.hRightOption);
  408.  
  409.     gPrintSettings.footerStrLeft   = hfIdToValue(gDialog.fLeftOption);
  410.     gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption);
  411.     gPrintSettings.footerStrRight  = hfIdToValue(gDialog.fRightOption);
  412.  
  413.     gPrintSettings.printBGColors = gDialog.printBG.checked;
  414.     gPrintSettings.printBGImages = gDialog.printBG.checked;
  415.  
  416.     gPrintSettings.shrinkToFit   = gDialog.shrinkToFit.checked;
  417.  
  418.     var scaling = document.getElementById("scalingInput").value;
  419.     if (scaling < 10.0) {
  420.       scaling = 10.0;
  421.     }
  422.     if (scaling > 500.0) {
  423.       scaling = 500.0;
  424.     }
  425.     scaling /= 100.0;
  426.     gPrintSettings.scaling = scaling;
  427.  
  428.     if (gDoDebug) {
  429.       dump("******* Page Setup Accepting ******\n");
  430.       dump("print_margin_top    "+gDialog.topInput.value+"\n");
  431.       dump("print_margin_left   "+gDialog.leftInput.value+"\n");
  432.       dump("print_margin_right  "+gDialog.bottomInput.value+"\n");
  433.       dump("print_margin_bottom "+gDialog.rightInput.value+"\n");
  434.     }
  435.   }
  436.  
  437.   // set return value to "ok"
  438.   if (paramBlock) {
  439.     paramBlock.SetInt(0, 1);
  440.   } else {
  441.     dump("*** FATAL ERROR: No paramBlock\n");
  442.   }
  443.  
  444.   var flags = gPrintSettingsInterface.kInitSaveMargins |
  445.               gPrintSettingsInterface.kInitSaveHeaderLeft |
  446.               gPrintSettingsInterface.kInitSaveHeaderCenter |
  447.               gPrintSettingsInterface.kInitSaveHeaderRight |
  448.               gPrintSettingsInterface.kInitSaveFooterLeft |
  449.               gPrintSettingsInterface.kInitSaveFooterCenter |
  450.               gPrintSettingsInterface.kInitSaveFooterRight |
  451.               gPrintSettingsInterface.kInitSaveBGColors |
  452.               gPrintSettingsInterface.kInitSaveBGImages |
  453.               gPrintSettingsInterface.kInitSaveInColor |
  454.               gPrintSettingsInterface.kInitSaveReversed |
  455.               gPrintSettingsInterface.kInitSaveOrientation |
  456.               gPrintSettingsInterface.kInitSaveOddEvenPages |
  457.               gPrintSettingsInterface.kInitSaveShrinkToFit |
  458.               gPrintSettingsInterface.kInitSaveScaling;
  459.  
  460.   gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
  461.  
  462.   return true;
  463. }
  464.  
  465. //---------------------------------------------------
  466. function onCancel()
  467. {
  468.   // set return value to "cancel"
  469.   if (paramBlock) {
  470.     paramBlock.SetInt(0, 0);
  471.   } else {
  472.     dump("*** FATAL ERROR: No paramBlock\n");
  473.   }
  474.  
  475.   return true;
  476. }
  477.  
  478.